home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / msql-1.0.6 / src / msql / net.c < prev    next >
C/C++ Source or Header  |  1995-02-14  |  5KB  |  250 lines

  1. /*
  2. **    net.c    - 
  3. **
  4. **
  5. ** Copyright (c) 1993  David J. Hughes
  6. **
  7. ** Permission to use, copy, and distribute for non-commercial purposes,
  8. ** is hereby granted without fee, providing that the above copyright
  9. ** notice appear in all copies and that both the copyright notice and this
  10. ** permission notice appear in supporting documentation.
  11. **
  12. ** This software is provided "as is" without any expressed or implied warranty.
  13. **
  14. ** ID = "$Id:"
  15. **
  16. */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include <arpa/inet.h>
  24. #include <signal.h>
  25. #include <netdb.h>
  26.  
  27. #include <common/portability.h>
  28. #include "msql_priv.h"
  29.  
  30.  
  31. static     u_char    packetBuf[PKT_LEN + 4];
  32. static    int    readTimeout;
  33. u_char    *packet = NULL;
  34.  
  35.  
  36. intToBuf(cp,val)
  37.         u_char  *cp;
  38.         int     val;
  39. {
  40.         *cp++ = (unsigned int)(val & 0x000000ff);
  41.         *cp++ = (unsigned int)(val & 0x0000ff00) >> 8;
  42.         *cp++ = (unsigned int)(val & 0x00ff0000) >> 16;
  43.         *cp++ = (unsigned int)(val & 0xff000000) >> 24;
  44. }
  45.  
  46.  
  47. bufToInt(cp)
  48.         u_char  *cp;
  49. {
  50.         int val;
  51.  
  52.         val = 0;
  53.         val = *cp++;
  54.         val += ((int) *cp++) << 8 ;
  55.         val += ((int) *cp++) << 16;
  56.         val += ((int) *cp++) << 24;
  57.         return(val);
  58. }
  59.  
  60.  
  61. initNet()
  62. {
  63.     packet = (u_char *)packetBuf + 4;
  64. }
  65.  
  66.  
  67.  
  68. void writePkt(fd)
  69.     int    fd;
  70. {
  71.     u_char    *cp;
  72.     int    len,
  73.         offset,
  74.         remain,
  75.         numBytes;
  76.  
  77.     len = strlen(packet);
  78.     intToBuf(packetBuf,len);
  79.     offset = 0;
  80.     remain = len+4;
  81.     while(remain > 0)
  82.     {
  83.         numBytes = write(fd,packetBuf + offset, remain);
  84.         if (numBytes == -1)
  85.         {
  86.             return;
  87.         }
  88.         offset += numBytes;
  89.         remain -= numBytes;
  90.     }
  91. }
  92.  
  93.  
  94. RETSIGTYPE alarmHandler(sig)
  95.     int    sig;
  96. {
  97.     readTimeout = 1;
  98. }
  99.  
  100.  
  101. int readPkt(fd)
  102.     int    fd;
  103. {
  104.     u_char    *c,
  105.         buf[4];
  106.     int    len,
  107.         remain,
  108.         offset,
  109.         numBytes;
  110.     static    int init = 1;
  111.  
  112. #ifdef MSQL_SERVER
  113.     if (init)
  114.     {
  115.         signal(SIGALRM,alarmHandler);
  116.         init = 0;
  117.     }
  118.     alarm(10);
  119. #endif
  120.     readTimeout = 0;
  121.     remain = 4;
  122.     offset = 0;
  123.     while(remain > 0)
  124.     {
  125.         numBytes = read(fd,buf + offset,remain);
  126.         if(numBytes <= 0)
  127.         {
  128.             alarm(0);
  129.                  return(-1);
  130.         }
  131.         remain -= numBytes;
  132.         offset += numBytes;
  133.         
  134.     }
  135. #ifdef MSQL_SERVER
  136.     if (readTimeout)
  137.     {
  138.         alarm(0);
  139.         return(-1);
  140.     }
  141. #endif
  142.     len = bufToInt(buf);
  143.     if (len > PKT_LEN)
  144.     {
  145.         fprintf(stderr,"Packet too large (%d)\n", len);
  146.         alarm(0);
  147.         return(-1);
  148.     }
  149.     remain = len;
  150.     offset = 0;
  151.     while(remain > 0)
  152.     {
  153.         numBytes = read(fd,packet+offset,remain);
  154.         if (readTimeout)
  155.         {
  156.             alarm(0);
  157.             return(-1);
  158.         }
  159.         if (numBytes <= 0)
  160.         {
  161.             alarm(0);
  162.                  return(-1);
  163.         }
  164.         remain -= numBytes;
  165.         offset += numBytes;
  166.     }
  167.     *(packet+offset) = 0;
  168. #ifdef MSQL_SERVER
  169.     alarm(0);
  170. #endif
  171.         return(len);
  172. }
  173.  
  174.  
  175.  
  176. /***********************************************************************
  177.  * 
  178.  * This section of code contains machine-specific code for handling integers.
  179.  * Msql supports 32 bit 2's complement integers.  To hide the details of
  180.  * converting integers for specific machines, the routines packInt32() and
  181.  * unpackInt32() were written.  If you have a machine that has native ints
  182.  * other than 32 bit 2's complement, you must either write your own versions
  183.  * of packInt32() and unpackInt32(), or modify the supplied ones.  For any
  184.  * machine using 2's complement ints, simple changes to the
  185.  * BYTES_PER_INT, HIGH_BITS, HIGH_BITS_MASK, and SIGN_BIT_MASK macros should
  186.  * make your code work.  If you have something else, you're on your own ...
  187.  *
  188.  ************************************************************************/
  189.  
  190. #if _CRAY
  191. #define BYTES_PER_INT    8
  192. #endif
  193.  
  194. #ifndef BYTES_PER_INT
  195. #define BYTES_PER_INT    4        /* default.  most boxes fit here */
  196. #endif
  197.  
  198. #if BYTES_PER_INT == 8
  199. #define BIG_INTS    1
  200. #define HIGH_BITS    32            /* bits-per-int minus 32 */
  201. #define HIGH_BITS_MASK    0xffffffff00000000    /* mask of the high bits */
  202. #define SIGN_BIT_MASK    0x0000000080000000      /* mask of your sign bit */
  203. #endif
  204.  
  205. #if BYTES_PER_INT == 4
  206. #define BIG_INTS          0
  207. #endif
  208.  
  209. /*
  210.  * Pack a native integer into a character buffer.  The buffer is assumed
  211.  * to be at least 4 bytes long.
  212.  */
  213.  
  214. int
  215. packInt32(num, buf)
  216. int    num;
  217. char    *buf;
  218. {
  219. #if BIG_INTS
  220.     num <<= HIGH_BITS;
  221. #endif
  222.  
  223.     bcopy((char *)&num, buf, 4);
  224.     return 0;
  225. }
  226.  
  227. /*
  228.  * Extract a native integer from a character buffer.  The buffer is assumed
  229.  * to have been formatted using the packInt32() routine.
  230.  */
  231.  
  232. int
  233. unpackInt32(buf)
  234. char    *buf;
  235. {
  236.     int    num;
  237.  
  238.     bcopy(buf, (char *)&num, 4);
  239.  
  240. #if BIG_INTS
  241.     num >>= HIGH_BITS;
  242.  
  243.     if (num & SIGN_BIT_MASK) {
  244.         num |= HIGH_BITS_MASK;
  245.     }
  246. #endif
  247.  
  248.     return num;
  249. }
  250.